<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body>
           <div id="app">
               <el-button class="el-icon-circle-plus-outline"  @click="handleCreate(0)"  type="primary" round>添加一级菜单</el-button>
                <el-tree
                        :data="treeData"
                        show-checkbox
                        node-key="id"
                        default-expand-all
                        :expand-on-click-node="false">
                  <span class="custom-tree-node" slot-scope="{ node, data }">
                      <span>{{data.name}}</span>
                      <el-button class="el-icon-circle-plus-outline"  @click="handleCreate(data.id)" type="text">添加子级菜单</el-button>
                      <el-button @click="handleDelete(data.id)" type="text">删除菜单</el-button>
                      <el-button @click="handleUpdate(data.id)" type="text">修改菜单</el-button>
                  </span>
                </el-tree>
               <!-- 新增标签弹层 -->
               <div class="add-form">
                   <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                       <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                label-width="100px">
                           <el-row>
                               <el-col :span="12">
                                   <el-form-item label="菜单名称" prop="name">
                                       <el-input v-model="formData.name"/>
                                   </el-form-item>
                               </el-col>

                               <el-col :span="12">
                                   <el-form-item label="linkURL" prop="linkUrl">
                                       <el-input v-model="formData.linkUrl"/>
                                   </el-form-item>
                               </el-col>

                               <el-col :span="12">
                                   <el-form-item label="id(不可改)" prop="parentMenuId">
                                       <el-input v-model="formData.parentMenuId" readonly/>
                                   </el-form-item>
                               </el-col>
                           </el-row>
                       </el-form>
                       <div slot="footer" class="dialog-footer">
                           <el-button @click="dialogFormVisible = false">取消</el-button>
                           <el-button type="primary" @click="append()">确定</el-button>
                       </div>
                   </el-dialog>
               </div>

               <!-- 编辑标签弹层 -->
               <div class="add-form">
                   <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                       <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                                label-width="100px">
                           <el-row>
                               <el-col :span="12">
                                   <el-form-item label="菜单名称" prop="name">
                                       <el-input v-model="formData.name"/>
                                   </el-form-item>
                               </el-col>

                               <el-col :span="12">
                                   <el-form-item label="linkURL" prop="linkUrl">
                                       <el-input v-model="formData.linkUrl"/>
                                   </el-form-item>
                               </el-col>

                               <el-col :span="12">
                                   <el-form-item label="id(不可改)" prop="parentMenuId">
                                       <el-input v-model="formData.parentMenuId" readonly/>
                                   </el-form-item>
                               </el-col>
                           </el-row>


                       </el-form>
                       <div slot="footer" class="dialog-footer">
                           <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                           <el-button type="primary" @click="handleEdit()">确定</el-button>
                       </div>
                   </el-dialog>
               </div>

            </div>

</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>

<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<!--引入axios拦截器-->
<script src="../plugins/interceptor.js"></script>
<script>

    var vm = new Vue({
        el:"#app",
        data:{
            treeData:[],
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            readonly: true,
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                name: [{required: true, message: '表单名为必填项', trigger: 'blur'}],
                /*linkUrl: [{required: true, message: 'linkUrl', trigger: 'blur'}],*/
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findMenu();
        },
        methods:{
            // 重置表单
            resetForm() {
                this.formData = {}
            },
            // 弹出添加窗口
            handleCreate(data) {
                console.log()
                this.resetForm();
                this.dialogFormVisible = true;
                this.formData.parentMenuId = data;
            },

            //添加菜单
            append(){
                    this.$refs["dataAddForm"].validate((flag) => {
                        if (flag) {
                            console.log(this.formData);
                            //表单验证成功，发送异步请求到后台查询数据
                            axios.post("/menu/add.do",this.formData)
                                .then((response) => {
                                    console.log(response);
                                    if (response.data.flag) {
                                        //隐藏新增窗口
                                        this.dialogFormVisible = false
                                        //刷新页面
                                        this.findMenu();
                                    }
                                    //给用户提示，当添加成功和失败切换不同的颜色弹框
                                    this.$message({
                                        message: response.data.message,
                                        type: response.data.flag ? 'success' : 'error'
                                    });
                                }).catch((error) => {
                                console.log("添加失败")
                            })
                        } else {
                            this.$message.error("表单数据验证失败")
                            return false
                        }


                    })

                },
            //编辑
            handleEdit(row) {
                /*
                * 步骤：
                * 1.在这个方法里是为了提交用户修改过后的数据
                * 2.首先进行表单的校验，步骤就和新增的校验一样
                * 3.校验完成之后发送异步请求到后台进行数据的修改
                *
                * */
                //1.校验表单
                this.$refs["dataEditForm"].validate((valid) => {
                    if (valid) {
                        //校验通过，发送异步请求到后台修改数据信息

                        axios.post("/menu/updataById.do",this.formData).then((result)=>{
                            //隐藏编辑表单
                            this.dialogFormVisible4Edit=false
                            console.log(result);
                            if(result.data.flag){
                                //说明修改成功弹出消息提示框
                                this.$message({
                                    message: result.data.message,
                                    type: 'success'
                                });

                            }else{
                                this.$message({
                                    message: result.data.message,
                                    type: 'error'
                                });
                            }
                        }).finally(()=>{
                            //重新发送分页查询数据
                            this.findMenu()
                        })

                    } else {
                        //表单校验失败
                        this.$message.error("表单数据校验失败");
                        return false;
                    }
                })
            },
            // 弹出编辑窗口
            handleUpdate(id) {
                // alert(row)
                /**
                 * 步骤：
                 * 1.因为要编辑数据，所以肯定要数据的回显，发送异步请求，根据id去查询数据，
                 * 2.当查到数据之后这时候我们就要把隐藏的编辑表单给展示出来并且把查到的数据回显到表单中
                 * 3.当数据回显之后进行修改，修改完成后点击确定调用handleEdit方法
                 */
                axios.get("/menu/queryById.do?id=" + id).then((result) => {
                    console.log(result);
                    if (result.data.flag) {
                        //说明根据id查询成功
                        //显示出隐藏的编辑表单
                        this.dialogFormVisible4Edit = true
                        //将查出的结果赋值给formData，用于将数据回显到编辑表单上
                        this.formData = result.data.data;
                    } else {
                        this.$message.error("获取数据失败，请刷新当前页面");
                    }
                });
            },


            // 删除
            handleDelete(id) {
                console.log(id);
                this.$confirm('此操作将永久删除该菜单及子菜单, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //走这一步说明用户点击的是确定按钮，然后开始发送异步请求去删除数据，因为删除时只携带一个id过去所以使用get的请求方式
                    axios.get("/menu/deleteById.do?id="+id).then((result) => {
                        console.log(result);
                        if (!result.data.flag) {
                            //说明删除失败,给用户提示删除失败信息
                            this.$message({
                                message: result.data.message,
                                type: 'error'
                            });
                        } else {
                            //说明删除数据成功，给用户提示信息，
                            this.$message({
                                message: result.data.message,
                                type: 'success'
                            });
                            //然后调用分页查询方法重新刷新页面
                            this.findMenu();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            //显示所有菜单
            findMenu(){
                //发送异步请求去后台查询数据
                axios.post("/menu/findMenu.do").then((result) => {
                    console.log(result);
                    this.treeData = result.data.data;
                })
            }

        }
    })

</script>
</html>